<?php

require_once 'Framework/Controller/Action.php';

class ChatoptionsController extends Framework_Controller_Action
{
	private $_smiles;
	private $_conf;
	
    public function init()
    {
    	if (!$this->session->get('authorize')) {
			$this->_redirect('/admin/');
		}
		$this->_conf = Framework_Registry::get('config');
		$this->view->adminnum = $this->session->get('adminnum');
		$this->view->current_page="chatopt";
		$this->_smiles = new Addon();
		
    }
    
    public function indexAction()
    {
        $this->render();
    }
	
	public function emotionsAction()
	{
		$i =  intval($this->_getParam(0,1));
		$this->session->set('smile_type',$i);
		
		$this->view->smiles = $this->_smiles->getAllSmiles($i);
		$this->render();
	}
	
	public function emoedittextAction()
	{
		$this->session->set('emoid',intval($this->_getParam(0)));	
		$this->view->smiles = $this->_smiles->
			getAllSmiles($this->session->get('smile_type'));	
		
		if ($this->getRequest()->getMethod() != 'POST') {	
			$this->view->id = intval($this->_getParam(0));		
		} else {
			$new_text = get_value($this->getRequest()->getParam('text'));
			$this->_smiles->setSmile($this->session->get('emoid'),$new_text);
			$this->_redirect('/admin/chatoptions/emotions/' . 
								$this->session->get('smile_type'));
		}
		$this->render();
	}
	
	public function updatesmileAction()
	{
		$i = $this->session->get('smile_type');
		$files = Addon::readSmile($this->_conf->smile->smile_dir,$i);
		Addon::updateSmile($files,$i);
		$this->_redirect('/admin/chatoptions/emotions/'.$i);
	}
	
	public function allusertableAction()
	{
		
	}
	
	public function phrasesAction()
	{
		$this->view->smiles = $this->_smiles->getAllPhrases();
		$this->render();
	}
	
	public function edittextAction()
	{
		if ($this->getRequest()->getMethod() != 'POST') {
			$i = ($this->_getParam(0)==0) ? 
				$this->_smiles->newPhrase() : intval($this->_getParam(0));
			$this->session->set('phid',$i);	
			$this->view->smiles = $this->_smiles->getAllPhrases();	
			$this->view->id = intval($i);		
		} else {
			$new_text = get_value($this->getRequest()->getParam('text'));
			$this->_smiles->setPhrase($this->session->get('phid'),$new_text);
			$this->_redirect('/admin/chatoptions/phrases/');
		}
		$this->render();
	}
	
	public function wappushAction()
	{
		$meta = Meta::getInstance();
        $meta->load('wap_push_url');
		$meta->load('wap_url');
		$meta->load('wap_msisdn');
		$meta->load('wap_message');
		$this->view->wap_push_url = $meta->wap_push_url;
		$this->view->wap_url = $meta->wap_url;
		$this->view->wap_msisdn = $meta->wap_msisdn;
		$this->view->wap_message = $meta->wap_message;
		if ($this->getRequest()->getMethod() == 'POST'){
			$wap_push_url = get_value($this->getRequest()->getParam('wap_push_url'));
			$wap_url = get_value($this->getRequest()->getParam('wap_url'));
			$wap_msisdn = get_value($this->getRequest()->getParam('wap_msisdn'));
			$wap_message = get_value($this->getRequest()->getParam('wap_message'));
			
			$meta->set('wap_push_url',$wap_push_url);
			$meta->load('wap_push_url');
			
			$meta->set('wap_url',$wap_push_url);
			$meta->load('wap_url');
			$meta->set('wap_msisdn',$wap_push_url);
			$meta->load('wap_msisdn');
			$meta->set('wap_message',$wap_push_url);
			$meta->load('wap_message');
			
			$this->view->wap_push_url = $meta->wap_push_url;
			$this->view->wap_url = $meta->wap_url;
			$this->view->wap_msisdn = $meta->wap_msisdn;
			$this->view->wap_message = $meta->wap_message;
		}
		$this->render();
	}
}
